var net = require('net');
const Signature = require('../command/base/Signature');
const signature = new Signature();
// 指定连接的tcp server ip，端口
var options = {
    host : '14.10.25.203',  
    port : 8080
}

var tcp_client = net.Socket();

// 连接 tcp server
tcp_client.connect(options,function(){
    console.log('connected to Server');
    tcp_client.write('I am tcp_client of node!');
})

// 接收数据
tcp_client.on('data',function(data){
    // console.log('received data: %s from server', data.toString());
    console.log(data);
})

tcp_client.on('end',function(){
    console.log('data end!');
})

tcp_client.on('error', function () {
    console.log('tcp_client error!');
})

// headerBuf.writeUInt8(86,0);
// headerBuf.writeUInt8(90,1);
// headerBuf.writeUInt8(isKeepAlive,2);
// headerBuf.writeUInt8(packId,3);
// headerBuf.writeUInt32BE(cmd.length,4);
// const bodyBuf = Buffer.alloc(cmd.length);
// bodyBuf.write(cmd);
// socket.write(headerBuf);
var headerBuf = Buffer.from([0x6a,0xa6]);
var lengthBuf = Buffer.from([0x05])
var controlBuf = Buffer.from([0x01]);
var dataBuf1 = Buffer.from([0x01]);
var dataBuf2 = Buffer.alloc(2);
dataBuf2.writeUInt16BE(64,0);
var crc = Buffer.concat([lengthBuf,controlBuf,dataBuf1,dataBuf2]);
var checksum = 0;
crc.forEach(element => {
    checksum+= element;
});

var checkBuf = Buffer.from(['0x'+checksum.toString(16)]);

var totalBuf = Buffer.concat([headerBuf,lengthBuf,controlBuf,dataBuf1,dataBuf2,checkBuf]);

console.log(totalBuf);
tcp_client.write(totalBuf);